package com.dolphin.servicedriveruser.service;

import com.dolphin.common.dto.Car;
import com.dolphin.common.dto.ResponseResult;
import com.dolphin.common.response.TerminalResponse;
import com.dolphin.common.response.TrackResponse;
import com.dolphin.servicedriveruser.mapper.CarMapper;
import com.dolphin.servicedriveruser.remote.ServiceMapClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CarService {
    @Autowired
    private CarMapper carMapper;

    @Autowired
    private ServiceMapClient serviceMapClient;
    public ResponseResult addCar(Car car) {
        car.setGmtCreate(LocalDateTime.now());
        car.setGmtModified(LocalDateTime.now());
        carMapper.insert(car);

        //获得此车辆终端ID 对应的tid
        ResponseResult<TerminalResponse> responseResult = serviceMapClient.addTerminal(car.getVehicleNo(),car.getId()+"");
        String tid = responseResult.getData().getTid();
        car.setTid(tid);

        //获得此车辆的轨迹id: trid
        ResponseResult<TrackResponse> trackResponseResponseResult = serviceMapClient.addTrack(tid);
        String trid = trackResponseResponseResult.getData().getTrid();
        String trname = trackResponseResponseResult.getData().getTrname();

        car.setTrid(trid);
        car.setTrname(trname);

        carMapper.updateById(car);
        return ResponseResult.success();
    }

    public ResponseResult<Car> getCarById(Long carId) {
        Car car = carMapper.selectById(carId);
        return ResponseResult.success(car);
    }
}
